import re
import requests

url = 'https://api.codemao.cn/api/fanfic/25867'
chapter_list = []
response = requests.get(url)
json_data = response.json()
temp = json_data['data']['fanficInfo']
description = [temp['title'], '作品类别：'+temp['fanfic_type_name'],
               '作者：'+temp['nickname'], '剧情简要：'+temp['introduction']]
data = json_data['data']['sectionList']
for i in data:
    chapter_list.append(
        'https://api.codemao.cn/api/fanfic/section/'+str(i['id']))

with open('限界：程式突破.txt', 'w', encoding='utf-8') as f:
    f.write('\n'.join(description))
    for i in chapter_list:
        response = requests.get(i)
        json_data = response.json()
        title = json_data['data']['section']['title']
        data = json_data['data']['section']['content']
        data = data.replace('&nbsp;', '')
        rule = re.compile(r'<p>(.*?)</p>')
        text = rule.findall(data)[2:]
        text = ''.join(text)
        f.write(title+'\n'+text+'\n')